package cn.com.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import cn.com.R;
import cn.com.domain.ChartMsgEntity;

public class ChatMsgViewAdapter extends BaseAdapter{
	
	public static interface IMsgViewType 
	{
		int IMVI_COM_MSG = 0 ;
		int IMVI_TO_MSG = 1 ;
	}
	
	private static final String TAG  = ChatMsgViewAdapter.class.getSimpleName();
	private List<ChartMsgEntity> coll ;
	private Context ctx ;
	private LayoutInflater mInflater ;
	
	public ChatMsgViewAdapter(Context context, List<ChartMsgEntity> coll){
		ctx = context;
		this.coll = coll ;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return coll.size();
	}

	@Override
	public Object getItem(int position) {
		return coll.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public int getItemViewType(int position) {
		ChartMsgEntity entity = coll.get(position);
		if (entity.getMsgType())
			return IMsgViewType.IMVI_COM_MSG;
		else
			return IMsgViewType.IMVI_TO_MSG;
		
	}
	public int getViewTypeCount() {
		return 2 ;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ChartMsgEntity entity = coll.get(position);
		boolean isComMsg = entity.getMsgType();
		ViewHolder  viewHolder = null ;
		if(convertView == null ){
			if(isComMsg){
				convertView = mInflater.inflate(R.layout.chatting_item_msg_text_left, null);
			}else{
				convertView = mInflater.inflate(R.layout.chatting_item_msg_text_right, null);
			}
			viewHolder = new ViewHolder();
			viewHolder.tvSendTime = (TextView) convertView.findViewById(R.id.tv_sendtime);
			viewHolder.tvUserName = (TextView) convertView.findViewById(R.id.tv_username);
			viewHolder.tvContent = (TextView) convertView.findViewById(R.id.tv_chatcontent);
			viewHolder.isComMsg = isComMsg;
			
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		viewHolder.tvSendTime.setText(entity.getDate());
		viewHolder.tvUserName.setText(entity.getName());
		viewHolder.tvContent.setText(entity.getText());
		return convertView;
	}
	
	static class  ViewHolder{
		public TextView tvSendTime ;
		public TextView tvUserName;
		public TextView tvContent ;
		public boolean isComMsg = true ;
	}

}
